<template>
  <div class="params">
    <el-breadcrumb separator="/" style="margin-bottom: 20px;">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card" style="line-height: 0; text-align: left;">
      <div class="title ">
        <p class="el-icon-warning"></p>
        注意:只允许为第三级设置参数
      </div>
      <el-cascader v-model="value" :options="paramsList" :props="{
        label: 'cat_name',
        value: 'cat_id',
        children: 'children'
      }" @change="getParams" style="margin-left: 100px;"></el-cascader>
      <!-- 默认选中第一个 -->
      <el-tabs :tab-position="tabPosition = 'top'" style="height: 200px;" active-name="first">
        <el-tab-pane label="动态参数" name="first">动态参数</el-tab-pane>
        <el-tab-pane label="静态参数" name="second">静态参数</el-tab-pane>
      </el-tabs>
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row v-for="(item, index) in scope.row.children" style="border-bottom: 1px lightslategray solid;"
              class="wyt-row">
              <el-col :span="4">
                <el-tag type="primary" @click="handleClick(item, scope.row.id)">{{ item.authName }}<p
                    class="iconfont el-icon-close" @click="delTag(index, scope.row.id)"></p>
                </el-tag>
              </el-col>
              <el-col :span="20">
                <el-row v-for="(item2, index2) in item.children">
                  <el-col :span="8">
                    <el-tag type="success" @click="handleClick(item2, scope.row.id)">{{ item2.authName }}
                      <p class="iconfont el-icon-close" @click="delTag(index2, scope.row.id)"></p>
                    </el-tag>
                  </el-col>
                  <el-col :span="16">
                    <el-tag type="info" v-for="(item3, index3) in item2.children"
                      @click="handleClick(item3, scope.row.id)">{{ item3.authName }}
                      <p class="iconfont el-icon-close" @click="delTag(index3, scope.row.id)"></p>
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="">
          <!-- 箭头 -->
          <template slot-scope="scope">
            <el-icon>
              <arrow-right />
            </el-icon>
          </template>
        </el-table-column>
        <el-table-column label="" prop="id">
        </el-table-column>
        <el-table-column label="角色名称" prop="roleName">
        </el-table-column>
        <el-table-column label="描述" prop="roleDesc">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="editRole(scope.row.id)">编辑</el-button>
            <el-button type="danger" @click="delRole(scope.row.id)">删除</el-button>
            <el-button type="info" @click="setRole(scope.row.id, scope.row.children)">设置</el-button>

          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>
<script>
import axios from 'axios';
export default {
  data() {
    return {
      paramsList: [],
      baseURL: 'http://43.138.15.137:7000/api/private/v1',
      token: localStorage.getItem('token'),
      value: [],
      // id
      catId: 0,
      // 商品参数
      ruleForm: {
        attrs: []
      }
    }
  },
  created() {
    this.getCategoryList()


  },
  methods: {
    async getCategoryList() {
      const res = await axios.get(this.baseURL + '/categories', {
        params: {
          type: 3,
          pagesize: 100,
          pagenum: 1,
        },
        headers: {
          'Authorization': this.token
        }
      }).then(res => {
        let { meta, data } = res.data
        this.paramsList = data.result
      })
    },
    fetchAttributes(catId, selval = 'many', targetArray = this.ruleForm.attrs) {
      axios.get(this.baseURL + '/categories/' + catId + '/attributes', {
        params: {
          sel: selval
        },
        headers: {
          'Authorization': this.token
        }
      }).then(res => {
        console.log(res);
        let { data: { data, meta } } = res
        targetArray.splice(0, targetArray.length, ...data); // 清空目标数组并填充新数据
        data.forEach(item => {
          item.attr_vals = item.attr_vals.split(',')
          item.attr_vals = item.attr_vals.map(item => {
            return {
              val: item,
              isck: false
            }
          })
        });
      })
    },
    getParams() {
      this.catId = this.value[this.value.length - 1]
      this.fetchAttributes(this.catId)
    }
  }

}
</script>
<style>
.title p {
  color: rgb(239, 183, 86);
  margin-left: 20px;
}

.title {
  width: 100%;
  background-color: rgba(239, 183, 86, 0.24);
  height: 40px;
  display: flex;
  justify-content: left;
  align-items: center;
  border-radius: 5px;
  margin-bottom: 20px;
  color: rgb(239, 183, 86);
}
</style>